<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model;

class UserVipRecord extends Model
{
    protected function getOptions(): array
    {
        return [
            'autoWriteTimestamp' => true,
            'jsonAssoc'          => true,
        ];
    }

    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo('User', 'user_id');
    }

    /**
     * 关联VIP等级
     */
    public function vip()
    {
        return $this->belongsTo('UserVip', 'vip_id');
    }

    public function channel()
    {
        return $this->belongsTo("Channel", "channel_id");
    }

    public function channelAccount()
    {
        return $this->belongsTo("ChannelAccount", "channel_account_id");
    }

    /**
     * 获取用户的VIP记录
     * @param int $userId 用户ID
     * @param bool $onlyValid 是否只获取有效记录
     * @return array
     */
    public static function getUserVipRecords($userId, $onlyValid = false)
    {
        $query = self::where('user_id', $userId);

        if ($onlyValid) {
            $query->where('status', 1)
                ->where('end_time', '>', date('Y-m-d H:i:s'));
        }

        return $query->order('create_time desc')
            ->select()
            ->toArray();
    }

    /**
     * 获取用户当前有效的VIP记录
     * @param int $userId 用户ID
     * @return array|null
     */
    public static function getCurrentVipRecord($userId)
    {
        $record = self::where('user_id', $userId)
            ->where('status', 1)
            ->where('end_time', '>', date('Y-m-d H:i:s'))
            ->order('end_time', 'desc')
            ->find();

        return $record ? $record->toArray() : null;
    }

    /**
     * 检查VIP记录是否有效
     * @return bool
     */
    public function isValid()
    {
        return $this->status == 1 && strtotime($this->end_time) > time();
    }

    /**
     * 使VIP记录失效
     * @return bool
     */
    public function invalidate()
    {
        $this->status = 0;
        return $this->save();
    }


    // 完成订单
    public function completeOrder($order)
    {
        $order->status       = 1;
        $order->order_status = 1;
        if ($order->start_time == 0) {
            $order->start_time = date('Y-m-d H:i:s');
        }
        $order->save();
        $vip  = $order->vip;
        $user = $order->user;
        // 赠送积分
        if ($vip->points_reward > 0) {
            $user->addPoints($vip->points_reward, 'vip_purchase', '购买VIP赠送积分');
        }
    }
}